﻿using System;
using System.Collections.Generic;
using System.Linq;
using LDM.Sys.Repository.Interfaces;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request.AuthorityReq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Model.Enum.ConfigModel;
using Utility.DB.SqlSugar;
using Utility.Extensions;
namespace LDM.Sys.Repository.Repository
{
    /// <summary>
    /// 用户角色关联仓储
    /// </summary>
    public class SysUserRoleRelRepository : DbContextBase<SysUserRoleRel>, ISysUserRoleRelRepository, IScoped
    {
        public SysUserRoleRelRepository(IConfiguration configuration) :
           base(configuration.GetConnectionString(ConnectionStringType.UserMysql.GetDescription()))
        {


        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<SysUserRoleRel> GetUserRoles(int userId)
        {
            return Db.Queryable<SysUserRoleRel>().Where(x => x.UserId == userId).ToList();
        }

        /// <summary>
        /// 获取角色用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<SysUserRoleRel> GetRoleUsers(int roleId)
        {
            return Db.Queryable<SysUserRoleRel>().Where(x => x.RoleId == roleId).ToList();
        }


        /// <summary>
        /// 获取角色用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<SysUserRoleRel> GetRoleUsers(List<int> roleIds)
        {
            return Db.Queryable<SysUserRoleRel>().Where(x => roleIds.Contains(x.RoleId)).ToList();
        }
    }
}
